#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/5/18 21:38
# @Author  : Beam
# @File    : rabbitmq-广播模式之fanout-publisher.py


import pika
credentials = pika.PlainCredentials('admin', 'admin')   #设置rabbitmq连接账号密码，默认是guest，guest
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.6.248',5672,'/',credentials))
channel = connection.channel()   #建立连接通道

channel.exchange_declare(exchange="logs",type="fanout")   #消费者端，这里跟生产者一样，等于收音机原理一样，大家调到同一个频道才能听
result = channel.queue_declare(exclusive=True)    #不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
queue_name = result.method.queue   #获取当前这个队列名
channel.queue_bind(exchange="logs",queue=queue_name)
print("waitting message...")
def callback(ch,method,properties,body):
    print("--> %s" %body)
channel.basic_consume(callback,queue=queue_name,no_ack=True)
channel.start_consuming()

